package com.funny.learning.english.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.funny.learning.english.BaseActivity;
import com.funny.learning.english.R;
import com.funny.learning.english.adapter.WellcomeAdapter;
import com.funny.learning.english.api.TiengAnhApi;
import com.funny.learning.english.api.BaseAjaxCallBack.IAjaxCallbackMMK;
import com.funny.learning.english.model.UserEntity;
import com.funny.learning.english.util.KeyHashUtil;
import com.funny.learning.english.util.PreferenceShUtil;
import com.sromku.simple.fb.Permission.Type;
import com.sromku.simple.fb.listeners.OnLoginListener;

public class WellcomeActivity extends BaseActivity implements OnClickListener{
	private ViewPager pager;
	private ImageView ivPagerPos;
	private ImageView ivLoginFace;
	private ImageView ivLoginGoogle;
	private ImageView ivLoginEmail;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wellcome_activity);
		KeyHashUtil.getAppKeyHash(this);
		pager = (ViewPager)findViewById(R.id.pager);
		ivPagerPos = (ImageView)findViewById(R.id.ivPagerPos);
		ivLoginFace = (ImageView)findViewById(R.id.ivLoginFace);
		ivLoginGoogle = (ImageView)findViewById(R.id.ivLoginGoogle);
		ivLoginEmail = (ImageView)findViewById(R.id.ivLoginEmail);
		
		ivLoginFace.setOnClickListener(this);
		ivLoginGoogle.setOnClickListener(this);
		ivLoginEmail.setOnClickListener(this);
		
		WellcomeAdapter adapter = new WellcomeAdapter(WellcomeActivity.this);
		pager.setAdapter(adapter);
		
		pager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				if(arg0 == 0){
					ivPagerPos.setImageResource(R.drawable.ic_pager1);
				}else if(arg0 == 1){
					ivPagerPos.setImageResource(R.drawable.ic_pager2);
				}else if(arg0 == 2){
					ivPagerPos.setImageResource(R.drawable.ic_pager3);
				}else if(arg0 == 3){
					ivPagerPos.setImageResource(R.drawable.ic_pager4);
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.ivLoginFace:
			onClickBtSignInFace();
			break;
		case R.id.ivLoginGoogle:
			
			break;
		case R.id.ivLoginEmail:
			Intent intent = new Intent(WellcomeActivity.this, LoginActivity.class);
			startActivity(intent);
			finish();
			break;

		default:
			break;
		}
	}
	
	private void onClickBtSignInFace() {
		String accessToken = getToken();
		if (!TextUtils.isEmpty(accessToken)) {
			TiengAnhApi.loginWithFacebook(accessToken, activity,
					loginWithFacebookAjaxCallbackMMK);

		} else {
			mSimpleFacebook.login(mOnLoginListener);
		}
	}
	
	private OnLoginListener mOnLoginListener = new OnLoginListener() {

		@Override
		public void onFail(String reason) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onException(Throwable throwable) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onThinking() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onNotAcceptingPermissions(Type type) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onLogin() {
			// TODO Auto-generated method stub
			TiengAnhApi.loginWithFacebook(getToken(), activity,
					loginWithFacebookAjaxCallbackMMK);

		}
	};
	
	private IAjaxCallbackMMK loginWithFacebookAjaxCallbackMMK = new IAjaxCallbackMMK() {

		@Override
		public <T> void handler(T t, int code) {
			// TODO Auto-generated method stub
			onHandler(t, code);
		}
	};
	
	private <T> void onHandler(T t, int code) {
		if (code == IAjaxCallbackMMK.COMPLETE) {
			String userEntity = (String) t;
			
			try {
				JSONObject jsonObject = new JSONObject(userEntity);
				
				JSONObject jsonUserInfo = jsonObject.getJSONObject("result");
				JSONObject jsonCoin = jsonObject.getJSONObject("coin");
				
				UserEntity entity = new UserEntity(jsonUserInfo, jsonCoin);
				appication.setUserEntity(entity);

//				if (cbSave.isChecked()) {
					PreferenceShUtil.setValue(activity, PreferenceShUtil.KEY_ACC,
							userEntity.toString());
//				}else{
//					PreferenceShUtil.setValue(activity, PreferenceShUtil.KEY_ACC,
//							"");
//				}

				startActivity(new Intent(activity, OptionActivity.class));
				finish();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			

		} else if (code == IAjaxCallbackMMK.ERROR) {
			Toast.makeText(activity, "Đăng nhập không thành công",
					Toast.LENGTH_SHORT).show();
		}
	}
}
